package com.yxs.wechatdemo.controller;

import com.yxs.wechatdemo.service.impl.WeChatServiceImpl;
import com.yxs.wechatdemo.utils.ResultVOUtil;
import com.yxs.wechatdemo.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by Administrator on 2018/10/8 0008.
 */
@Slf4j
@Controller
@RequestMapping("/wechat")
public class WeChatController {

    @Autowired
    private WeChatServiceImpl weChatService;

    @ResponseBody
    @GetMapping("/getSignInfo")
    public ResultVO getSignInfo(@RequestParam(required = false) String url) {
//        if(url == null) {
//            throw new ImitateException(ResultEnum.PARAM_NOT_EXIST);
//        }
        return ResultVOUtil.success(weChatService.getSignInfo("http://wx-arraon.natapp1.cc/home"));
    }

    @GetMapping("/getAuthorize")
    public String getAuthorize(@RequestParam(required = false) String returnUrl) {
        String url = weChatService.getAuthorize(returnUrl);
        return "redirect:" + url;
    }

    @GetMapping("/auth")
    public String auth(@RequestParam("code") String code, @RequestParam("state") String returnUrl) {
        String url = weChatService.getTokenUrl(code, returnUrl);
        return "redirect:" + url;
    }

}
